package com.muyi.service.impl;

import com.muyi.dao.CommentDao;
import com.muyi.dao.impl.CommentDaoImpl;
import com.muyi.pojo.dto.CommentDTO;
import com.muyi.pojo.entity.Comment;
import com.muyi.pojo.vo.Result;

import java.util.List;

public class CommentServiceImpl implements com.muyi.service.CommentService {
    private CommentDao commentDao = new CommentDaoImpl();

    @Override
    public Result addComment(Comment comment) throws Exception {
        //添加评论信息
        int i = commentDao.addComment(comment);
        if (i > 0) {
            return new Result();
        }
        return new Result(-1, "添加评论失败");
    }

    @Override
    public Result deleteCommentById(Integer comment_id) throws Exception {
        int i = commentDao.deleteCommentById(comment_id);
        if (i == 0) {
            return new Result(-1, "删除评论失败");
        }
        return new Result();
    }

    @Override
    public Result updateComment(Comment comment) throws Exception {
        int i = commentDao.updateComment(comment);
        if (i == 0) {
            return new Result(-1, "修改评论信息失败");
        }
        return new Result();
    }

    @Override
    public Result getCommentById(Integer comment_id) throws Exception {
        Comment comment = commentDao.getCommentById(comment_id);
        if (null == comment) {
            return new Result(-1, "查询评论信息失败");
        }
        Result result = new Result();
        result.setData(comment);
        return result;
    }

    @Override
    public Result getCommentBySearch(CommentDTO commentdto, Integer pageNo, Integer pageSize) throws Exception {
        //动态查询评论信息
        List<CommentDTO> commentdtoList = commentDao.getCommentBySearch(commentdto, pageNo, pageSize);
        //查询总数量
        long total = commentDao.getTotal(commentdto);
        //封装result
        Result result = new Result();
        result.setData(commentdtoList);
        result.setTotal(total);
        return result;
    }

    public Result deleteCommentByIds(Integer[] comment_ids) throws Exception {
        int comment_id = commentDao.deleteCommentByIds(comment_ids);
        if (comment_id == 0) {
            return new Result(-1, "删除评论信息失败");
        }
        return new Result(1, "删除评论信息成功");
    }
}
